﻿using System;
using System.Data;
using System.Web;
using System.Web.UI.WebControls;
using System.Text;
using PlugNT.Cms.Core;
using PlugNT.Cms.BLL;
using PlugNT.Cms.Model;
using PlugNT.Cms.Page;

namespace PlugNT.Cms.Web.Admin.Pre_Adminer
{
    public partial class Add : AdminPage
    {
        private Adminer curAdminer = new Adminer();
        private GroupAdmin curGroupAdmin = new GroupAdmin();
        protected void Page_Load(object sender, EventArgs e)
        {
            //权限检测
            this.CheckPower();

            if (!IsPostBack)
            {
                for (int i = 18; i < 100; i++)
                {
                    ListItem li = new ListItem(i.ToString() + "岁", i.ToString());
                    ddlAge.Items.Add(li);
                }
                FillPowerList();
            }
        }

        private void FillPowerList()
        {
            ddlPower.Items.Clear();
            ddlPower.Items.Add(new ListItem("超级管理员", "0")); ;
            DataTable dt = curGroupAdmin.GetPowerList();
            foreach (DataRow dr in dt.Rows)
            {
                ddlPower.Items.Add(new ListItem(dr["name"].ToString(), dr["power"].ToString()));
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            AdminerInfo model = new AdminerInfo();
            string username = txtName.Text.Trim();
            string errorMessage = "添加管理员--<b>[" + username + "]</b>--失败！&z=no";
            bool flag = false;
            if (curAdminer.Exists(username))
            {
                errorMessage = "添加管理员失败，--<b>[" + username + "]</b>--已存在！&z=no";
            }
            else
            {

                model.no = PlugNT.Common.Utils.GetDateTimeCode();
                model.name = username;
                model.password =curAdminer.MD5Hasher(txtPass.Text.Trim());
                model.money = 0;
                model.qq = txtQq.Text.Trim();
                model.email = txtEmail.Text.Trim();
                model.age = int.Parse(ddlAge.SelectedValue.Trim());
                model.logintime = DateTime.Now;
                model.sex = ddlSex.SelectedValue.Trim();
                model.pic = "none";
                model.power = ddlPower.SelectedValue.Trim();
                model.username = "none";
                flag = curAdminer.Add(model);
            }

            if (flag)
            {
                Response.Redirect("../Message.aspx?message=添加管理员--<b>[" + username + "]</b>--成功！&z=yes");
            }
            else
            {
                Response.Redirect("../Message.aspx?message=" + errorMessage);
            }
        }


    }
}